package com.iweb.servlet;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.iweb.poji.User;
import com.iweb.utils.JSONWriter;
import com.iweb.utils.RestParser;
import com.iweb.utils.UserTable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 接口设计
 */
@WebServlet(value = "/user/*")
public class RestServlet extends HttpServlet {

    private String pattern = "user";

    private UserTable table;

    /**
     * 容器启动的时候模拟数据库 创建一个user对象的表格
     *
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        table = new UserTable();
    }

    /**
     * 接受GET请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取id 如果id 为null 则说没没有参数
        Integer id = RestParser.id(req, pattern);
        if (ObjectUtil.isEmpty(id)) {
            List<User> users = table.select();
            JSONWriter.send(resp, users);
        } else {
            User user = table.select(id);
            JSONWriter.send(resp, user);
        }
    }

    /**
     * 接受POST请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = createUser(req);
        table.insert(user);
        JSONWriter.send(resp, null);
    }


    /**
     * 接受PUT请求 首先要获取ID
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = RestParser.id(req, pattern);
        if (!ObjectUtil.isEmpty(id)) {
            User user = createUser(req);
            user.setId(id);
            table.update(user);
            JSONWriter.send(resp, null);
        }
    }

    /**
     * 接受DELETE请求 首先要获取ID
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = RestParser.id(req, pattern);
        if (ObjectUtil.isNotEmpty(id)) {
            table.delete(id);
            JSONWriter.send(resp, null);
        }
    }

    private User createUser(HttpServletRequest req) {
        User user = new User();
        JSONObject jsonObject = RestParser.arg2json(req);
        user.setId(Convert.toInt(jsonObject.get("id")));
        user.setName(Convert.toStr(jsonObject.get("name")));
        user.setAge(Convert.toInt(jsonObject.get("age")));
        return user;
    }
}
